
(function($){
	var map;
	var geocoder;
	var markersArray = [];
	var infowindow;
	Gmap = {
		options :{
				initOption:
				{
				  zoom: 4,
				  center: new google.maps.LatLng(40.230708, 105.472916),
				  mapTypeId: google.maps.MapTypeId.ROADMAP
				},
				markerOption:
				{
					draggable:true,
					clickable:true
				}
		},
		initMap:function(id,myoptions)
		{
			myoptions = $.extend({},this.options.initOption,myoptions);
			map = new google.maps.Map(document.getElementById(id), myoptions);
			geocoder   = new google.maps.Geocoder();
			infowindow = new google.maps.InfoWindow();
		},
		createMarker:function(myoptions,is_model,func,rfunc)
		{
			myoptions = $.extend(this.options.markerOption,myoptions,{map:map});
			$this = this;
			if(!is_model)
			{
				this.deleteOverlays();
			}
			var marker =  new google.maps.Marker(myoptions);
			markersArray.push(marker);
			google.maps.event.addListener(marker, 'click', function(e) {
				if(!func)
				{
					latlng = marker.getPosition();
	        	  	$this.getAddrBySite(latlng,function($cont){
	        	  		$this.showInfo($cont+" "+latlng,marker)
	        	  	});
				}
				else
				{
					func(marker);
				}
			});
			google.maps.event.addListener(marker, 'rightclick', function(e) {
				if(!rfunc)
				{
					if(confirm('你想删除这个标记吗？'))
					{
						marker.setMap(null);
						infowindow.close();
					}else
					{
						return false;
					}
				}
				else
				{
					rfunc(marker);
				}
			});
				
			return marker;
			//{   position: myLatlng, map: map,title:"Hello World!",draggable:true}
		},
		placeMarker:function(location) 
		{
			  var clickedLocation = new google.maps.LatLng(location);
			  var marker = new google.maps.Marker({
			      position: location, 
			      map: map
			  });
			  map.setCenter(location);
		},
		
		createInfoWindow:function(options)
		{
			return new google.maps.InfoWindow(options);
			// { content: message[number],size: new google.maps.Size(50,50) }
		},
		addMarker:function(location) 
		{
			  marker = new google.maps.Marker({
			    position: location,
			    map: map
			  });
			  markersArray.push(marker);
		},
		clearOverlays:function() 
		{
			  if (markersArray) 
			  {
				    for (i in markersArray) 
				    {
				      markersArray[i].setMap(null);
				    }
			  }
		},
		showOverlays:function() 
		{
			  if (markersArray) 
			  {
				    for (i in markersArray) 
				    {
				      markersArray[i].setMap(map);
				    }
			  }
		},
		deleteOverlays:function() 
		{
			  if (markersArray) 
			  {
				    for (i in markersArray) 
				    {
				    	markersArray[i].setMap(null);
				    }
				    markersArray.length = 0;
			  }
		},
		reverseAnalyze:function(input,zoom,func)
		{
		    var latlngStr = input.split(",");
		    var lat = parseFloat(latlngStr[0]);
		    var lng = parseFloat(latlngStr[1]);
		    var latlng = new google.maps.LatLng(lat, lng);
		    $this = this;
		    if (geocoder) 
		    {
		      geocoder.geocode({'latLng': latlng}, function(results, status) {
			        if (status == google.maps.GeocoderStatus.OK) 
			        {
				          if (results[1]) 
				          {
				            map.setZoom(zoom||11);
				            marker = $this.createMarker({
				                position: latlng, 
				                map: map
				            }); 
				            infowindow.setContent(results[1].formatted_address);
				            infowindow.open(map, marker);
				            if(func)
					            {
					            	func(marker);
					            }
				          }
			        } 
			        else 
			        {
			        	return "Geocoder failed due to: " + status;
			        }
		      });
		    }
		},
		getAddrBySite:function(latlng,func)
		{
		    $this = this;
		    if (geocoder) 
		    {
		      geocoder.geocode({'latLng': latlng}, function(results, status) {
			        if (status == google.maps.GeocoderStatus.OK) 
			        {
				          if (results[1]) 
				          {
				              func(results[1].formatted_address);
				          }
				          else
				          {
				        	  return false;
				          }
			        } 
			        else 
			        {
			        	return "Geocoder failed due to: " + status;
			        }
		      });
		    }
		},
		showInfo:function(content,marker)
		{
			infowindow.setContent(content);
			infowindow.open(map, marker);
		},
		clearInfo:function(marker)
		{
			infowindow.setContent("");
			infowindow.close();
		},
		searchAddress:function(id,func)
		{
			var address = document.getElementById(id).value;
			$this = this;
		    if (geocoder) 
		    {
		      geocoder.geocode( { 'address': address}, function(results, status) {
			        if (status == google.maps.GeocoderStatus.OK) 
			        {
				          map.setCenter(results[0].geometry.location);
				          var marker = $this.createMarker({
				              map: map, 
				              draggable:true,
				              position: results[0].geometry.location
				          });
				          
			        	  	$this.getAddrBySite(results[0].geometry.location,function($cont){
				        	  		func(marker,$cont,results[0].geometry.location)
			        	  	});
			          
			        } 
			        else 
			        {
			        	return "Geocode was not successful for the following reason: " + status;
			        }
		      });
		    }
			
		},
		setZoom:function(zoom)
		{
			map.setZoom(zoom);
		},
		fitBounds:function(bounds)
		{
			map.fitBounds(bounds);
		}
	}
})(jQuery)